﻿
//KNOCKOUT VALIDATION
ko.validation.configure({
        registerExtenders: true,
        messagesOnModified: true,
        insertMessages: false,
        parseInputAttributes: true,
        messageTemplate: null,
        grouping: { deep: true, observable: true }
    });

//TINYMCE
ko.bindingHandlers.tinymce = {
    init: function(element, valueAccessor, allBindingsAccessor, context) {
        var options = allBindingsAccessor().tinymceOptions || { };
        var modelValue = valueAccessor();
        var value = ko.utils.unwrapObservable(valueAccessor());
        var el = $(element);


        options.setup = function(ed) {

            ed.onChange.add(function(editor, l) { //handle edits made in the editor. Updates after an undo point is reached.
                if (ko.isWriteableObservable(modelValue)) {
                    modelValue(l.content);
                }
            });

            ed.onInit.add(function(ed, evt) { // Make sure observable is updated when leaving editor.
                var dom = ed.dom;
                var doc = ed.getDoc();
                tinymce.dom.Event.add(doc, 'blur', function(e) {
                    if (ko.isWriteableObservable(modelValue)) {
                        modelValue(ed.getContent({ format: 'raw' }));
                    }
                });
            });

        };

        //handle destroying an editor (based on what jQuery plugin does)
        ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
            $(element).parent().find("span.mceEditor,div.mceEditor").each(function(i, node) {
                var ed = tinyMCE.get(node.id.replace( /_parent$/ , ""));
                if (ed) {
                    ed.remove();
                }
            });
        });

        //$(element).tinymce(options);
        setTimeout(function() { $(element).tinymce(options); }, 0);
        el.val(value);

    },
    update: function(element, valueAccessor, allBindingsAccessor, context) {
        var el = $(element);
        var value = ko.utils.unwrapObservable(valueAccessor());
        var id = el.attr('id');

        //handle programmatic updates to the observable
        // also makes sure it doesn't update it if it's the same.
        // otherwise, it will reload the instance, causing the cursor to jump.
        if (id !== undefined && id !== '') {
            var content = tinyMCE.getInstanceById(id).getContent({ format: 'raw' });
            if (content !== value) {
                el.val(value);
            }
        }
    }
};
//TOOLTIP
ko.bindingHandlers.tooltip = {
    init: function (element, valueAccessor) {
        var options;
        options = ko.utils.unwrapObservable(valueAccessor());
        return $(element).tooltip(options);

//        $(element).attr('rel', 'tooltip');
//        return $(element).attr('title', options.title);
    },
    update: function (element, valueAccessor, allBindingsAccessor) { }
};
//POPOVER
ko.bindingHandlers.popover = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var options;
        options = ko.utils.unwrapObservable(valueAccessor());
        if (options.titleId) {
            var titleJquery = $($("#" + options.titleId).html());
            var titleObj = titleJquery.get(0);
            ko.applyBindings(bindingContext, titleObj);
            options.title = titleObj.outerHTML;
        }
        if (options.contentId) {
            var contentJquery = $($("#" + options.contentId).html());
            var contentObj = contentJquery.get(0);
            ko.applyBindings(bindingContext, contentObj);
            options.content = contentObj.outerHTML;
        }
        return $(element).popover(options);
        //        $(element).attr('rel', 'popover');
        //        $(element).attr('data-content', options.dataContent);
        //        return $(element).attr('data-original-title', options.dataOriginalTitle);
    },
    update: function (element, valueAccessor, allBindingsAccessor) { }
};
//DATEPICKER
ko.bindingHandlers.datepicker = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        $(element).datepicker({
            dateFormat: "yy-mm-dd",
            //dateFormat: "MM-yy",
            changeYear: true,
            changeMonth: true
        });

        //handle the field changing
        ko.utils.registerEventHandler(element, "change", function () {
            var observable = valueAccessor();
            observable($(element).datepicker("getDate"));
        });

        //handle disposal (if KO removes by the template binding)
        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            $(element).datepicker("destroy");
        });

    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor()),
            current = $(element).datepicker("getDate");

        if (value - current !== 0) {
            $(element).datepicker("setDate", value);
        }
    }
};

//ENTER KEY EVENT
ko.bindingHandlers.executeOnEnter = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var allBindings = allBindingsAccessor();
        $(element).keypress(function (event) {
            var keyCode = (event.which ? event.which : event.keyCode);
            if (keyCode === 13) {
                allBindings.executeOnEnter.call(viewModel);
                return false;
            }
            return true;
        });
    }
};
